package com.beone.smart.smartBeauty.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 描述类的功能
 *
 * @author su
 * 2019-08-19 16:36
 */
public class CommTemplate {


    /**
     * 创建通用模板 不包括数据填充
     *
     * @return List<Map < String, Object>> serial数据 map里面放 名称 数据
     * warnList 警戒线
     */
    public static Map<String, Object> creatTemplate(String title, List<String> legendData, List<String> xData,
                                                    String yMin, String yMax, String yUnit, List<Map<String, Object>> serialData,
                                                    List<String> warnList) {
        Map<String, Object> result = new HashMap<>();

        //头
        Map<String, Object> titleMap = new HashMap<>();
        titleMap.put("text", title);
        result.put("title", titleMap);

        //tooltip
        Map<String, Object> tooltipMap = new HashMap<>();
        tooltipMap.put("trigger", "axis");
        result.put("tooltip", tooltipMap);

        //数据(根据具体数据填充)
        Map<String, Object> legendMap = new HashMap<>();
        legendMap.put("data", legendData);
        result.put("legend", legendMap);

        //工具条
        Map<String, Object> toolboxMap = new HashMap<>();
        toolboxMap.put("show", true);

        Map<String, Object> typeMap = new HashMap<>();
        typeMap.put("type", Arrays.asList("line", "bar", "pie"));
        Map<String, Object> magicTypeMap = new HashMap<>();
        magicTypeMap.put("magicType", typeMap);
        toolboxMap.put("feature", magicTypeMap);
        result.put("toolbox", toolboxMap);

        //x轴数据
        Map<String, Object> xAxisMap = new HashMap<>();
        xAxisMap.put("type", "category");
        xAxisMap.put("boundaryGap", false);
        xAxisMap.put("data", xData);
        result.put("xAxis", xAxisMap);

        //y轴数据
        Map<String, Object> yAxisMap = new HashMap<>();
        yAxisMap.put("type", "value");
        yAxisMap.put("min", yMin);
        yAxisMap.put("max", yMax);
        Map<String, Object> axisLabelMap = new HashMap<>();
        axisLabelMap.put("formatter", "{value} " + yUnit);
        yAxisMap.put("axisLabel", axisLabelMap);
        result.put("yAxis", yAxisMap);

        List<Map<String, Object>> seriesList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(serialData)) {
            for (int i = 0; i < serialData.size(); i++) {
                Map<String, Object> indexMap = new HashMap<>();

                indexMap.put("name", serialData.get(i).get("name"));
                indexMap.put("type", "line");
                indexMap.put("data", serialData.get(i).get("data") != null ? serialData.get(i).get("data") : Arrays.asList());

                //警戒线放第一个数据节点下
                if (i == 0 && !CollectionUtils.isEmpty(warnList)) {
                    indexMap.put("markLine", createMarkLine(warnList));
                }
                seriesList.add(indexMap);
            }
        }
        result.put("series", seriesList);
        return result;
    }


    /**
     * 生产警戒线js
     *
     * @param yValue
     * @return
     */
    private static Map<String, Object> createMarkLine(List<String> yValue) {
        Map<String, Object> markLineMap = new HashMap<>();
        //去掉警戒线
        markLineMap.put("symbol", false);
        Map<String, Object> labelMap = new HashMap<>();
        labelMap.put("position", "start");
        markLineMap.put("label", labelMap);

        List<Map<String, Object>> dataList = new ArrayList<>();
        for (String value : yValue) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("silent", false);
            Map<String, Object> lineStyleMap = new HashMap<>();
            lineStyleMap.put("type", "solid");
            lineStyleMap.put("color", "#FA3934");
            dataMap.put("lineStyle", lineStyleMap);
            dataMap.put("yAxis", value);
            dataList.add(dataMap);
        }
        markLineMap.put("data", dataList);
        return markLineMap;
    }
}
